package com.winit.openapi.wh.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.oms.spi.inbound.InboundDispatchInfoService;
import com.winit.oms.spi.inbound.command.CreateInboundDispatchinfoCommand;
import com.winit.oms.spi.inbound.vo.InboundDispatchinfoVo;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.wh.builder.InboundPickupBuilder;
import com.winit.openapi.wh.validator.InboundPickupFieldValidator;

/**
 * 海外仓入库单 提货 验货
 * 
 * @version <pre>
 * Author     Version       Date        Changes
 * zq   1.0         2015年11月10日     Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/wh/pickup")
public class InboundPickupController extends BaseController {

    @Resource
    private InboundDispatchInfoService inboundDispatchInfoService;

    /**
     * 验货/提货信息 保存草稿
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/saveByDraft ")
    public String inspectionPickUpDraft(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        InboundDispatchinfoVo inboundDispatchinfoVo = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            InboundPickupFieldValidator.ValidInspectionPickUpByDraftField(json);
            inboundDispatchinfoVo = InboundPickupBuilder.buildInspectionPickUp(json);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        // 组装command
        CreateInboundDispatchinfoCommand command = new CreateInboundDispatchinfoCommand();
        command.setCtx(CommandContext.getContext());
        command.setInboundDispatchinfoVo(inboundDispatchinfoVo);
        // 保存信息
        inboundDispatchInfoService.saveDispatchinfoVo(command);
        return SUCCESS;
    }

    /**
     * 验货/提货信息 继续
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/nextStep")
    public String inspectionPickUpNext(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        InboundDispatchinfoVo inboundDispatchinfoVo = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            InboundPickupFieldValidator.ValidInspectionPickUpByNextField(json);
            inboundDispatchinfoVo = InboundPickupBuilder.buildInspectionPickUp(json);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        // 组装command
        CreateInboundDispatchinfoCommand command = new CreateInboundDispatchinfoCommand();
        command.setCtx(CommandContext.getContext());
        command.setInboundDispatchinfoVo(inboundDispatchinfoVo);
        // 保存信息
        inboundDispatchInfoService.saveDispatchinfoVo(command);
        return SUCCESS;
    }
}
